//
//  LLCachePathManager.m
//  LLFoundation
//
//  Created by my_liu on 2016/11/14.
//  Copyright © 2016年 . All rights reserved.
//

#import "LLCachePathManager.h"
#import "LLUitils.h"

#define LLCachePathManagerMainCacheDirectory @"LLCachePathManagerMainCacheDirectory"

#define LLCachePathManagerMainImageCacheDirectory @"LLCachePathManagerMainImageCacheDirectory"

#define LLCachePathManagerMainAudioCacheDirectory @"LLCachePathManagerMainAudioCacheDirectory"

static NSString *  LLAudioFileCacheSubTempEncodeFileDirectory = @"LLAudioFileCacheSubTempEncodeFileDirectory";

@implementation LLCachePathManager

+ (LLCachePathManager *)shareManager
{
    static LLCachePathManager *_pathManager = nil;
    static dispatch_once_t onceToken;
    LLDispatchOnce(onceToken, ^{
        _pathManager = [[self alloc]init];
    });
    return _pathManager;
}

- (instancetype)init
{
    if (self = [super init]) {
        
        [self setupCacheDirectorys];
    }
    return self;
}

- (void)setupCacheDirectorys
{
    /* 主缓存目录 */
    if (!LLFileDirectoryIsExist([self mainCacheDirectory])) {
        LLFileDirectoryCreate([self mainCacheDirectory]);
    }
    
    /* 主图片缓存目录 */
    if (!LLFileDirectoryIsExist([self mainImageCacheDirectory])) {
        LLFileDirectoryCreate([self mainImageCacheDirectory]);
    }
    
    /* 主音频缓存目录 */
    if (!LLFileDirectoryIsExist([self mainAudioCacheDirectory])) {
        LLFileDirectoryCreate([self mainAudioCacheDirectory]);
    }
}

- (NSString *)mainCacheDirectory
{
    return LLAppCachePath(LLCachePathManagerMainCacheDirectory);
}

- (NSString *)mainImageCacheDirectory
{
    return [[self mainCacheDirectory]stringByAppendingPathComponent:LLCachePathManagerMainImageCacheDirectory];
}

- (NSString *)mainAudioCacheDirectory
{
    return [[self mainCacheDirectory]stringByAppendingPathComponent:LLCachePathManagerMainAudioCacheDirectory];
}

/* 主图片缓存下文件路径 */
- (NSString *)mainImageCacheFilePath:(NSString *)fileName
{
    if (LLStringIsNull(fileName)) {
        return nil;
    }
    return [[self mainImageCacheDirectory]stringByAppendingPathComponent:fileName];
}

/* 主音频缓存下文件路径 */
- (NSString *)mainAudioCacheFilePath:(NSString *)fileName
{
    if (LLStringIsNull(fileName)) {
        return nil;
    }
    return [[self mainAudioCacheDirectory]stringByAppendingPathComponent:fileName];
}

/* 在主缓存目录下面创建或者返回指定名字的目录路径 */
- (NSString *)createOrGetSubCacheDirectoryWithName:(NSString *)dirName
{
    if (LLStringIsNull(dirName)) {
        return nil;
    }
    NSString *dirPath = [[self mainCacheDirectory] stringByAppendingPathComponent:dirName];
    if (LLFileDirectoryIsExist(dirPath)) {
        return dirPath;
    }else{
        LLFileDirectoryCreate(dirPath);
        return dirPath;
    }
}

/* 在主图片缓存目录下返回或者创建指定目录名字的目录路径 */
- (NSString *)createOrGetSubImageCacheDirectoryWithName:(NSString *)dirName
{
    if (LLStringIsNull(dirName)) {
        return nil;
    }
    NSString *dirPath = [[self mainImageCacheDirectory] stringByAppendingPathComponent:dirName];
    if (LLFileDirectoryIsExist(dirPath)) {
        return dirPath;
    }else{
        LLFileDirectoryCreate(dirPath);
        return dirPath;
    }
}

/* 在主音频缓存目录下返回或者创建指定目录名字的目录路径 */
- (NSString *)createOrGetSubAudioCacheDirectoryWithName:(NSString *)dirName
{
    if (LLStringIsNull(dirName)) {
        return nil;
    }
    NSString *dirPath = [[self mainAudioCacheDirectory] stringByAppendingPathComponent:dirName];
    if (LLFileDirectoryIsExist(dirPath)) {
        return dirPath;
    }else{
        LLFileDirectoryCreate(dirPath);
        return dirPath;
    }
}

/* 主图片缓存目录下是否存在名为fileName的文件 */
- (BOOL)mainImageCacheFileExist:(NSString *)fileName
{
    return LLFileIsExist([self mainImageCacheFilePath:fileName]);
}

/* 主音频缓存目录下是否存在名为fileName的文件 */
- (BOOL)mainAudioCacheFileExist:(NSString *)fileName
{
    return LLFileIsExist([self mainAudioCacheFilePath:fileName]);
}

/* 为一个图片链接地址返回缓存路径 */
- (NSString *)mainImageCacheFilePathForUrl:(NSString *)url
{
    if (LLStringIsNull(url)) {
        return nil;
    }
    NSString *fileName = [url stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
    return [self mainImageCacheFilePath:fileName];
}

/* 为一个语音地址返回缓存路径 */
- (NSString *)mainAudioCacheFilePathForUrl:(NSString *)url
{
    if (LLStringIsNull(url)) {
        return nil;
    }
    NSString *fileName = [url stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
    return [self mainAudioCacheFilePath:fileName];
}

/* 确定主图片缓存下是否有链接为url的文件缓存  */
- (BOOL)mainImageCacheFileIsExistForUrl:(NSString *)url
{
    return LLFileIsExist([self mainImageCacheFilePathForUrl:url]);
}

/* 确定主语音缓存下是否有链接为url的文件缓存 */
- (BOOL)mainAudioCacheFileIsExistForUrl:(NSString *)url
{
    return LLFileIsExist([self mainAudioCacheFilePathForUrl:url]);
}

- (NSString *)mainAudioTempEncodeFile:(NSString *)fileName
{
    return [[self createOrGetSubAudioCacheDirectoryWithName:LLAudioFileCacheSubTempEncodeFileDirectory]stringByAppendingPathComponent:fileName];
}


@end
